package com.kiln.juxingqs.fragment

import android.view.View
import android.widget.Toast
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.viewModelScope
import client.comm.baoding.R
import client.comm.baoding.adapter.MyGuaDAndSgAdapter
import client.comm.baoding.databinding.FragmentLoadmorepageBinding
import client.comm.baoding.dialog.JyInputNumDialog
import client.comm.baoding.dialog.MsgTitleDialog
import client.comm.baoding.ui.MyGuaDan2Activity
import client.comm.baoding.ui.MyGuaDanActivity
import client.comm.baoding.ui.vm.TradeDetailModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.base.PagerFooterAdapter
import client.comm.commlib.network.data.PagingJsonResult
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

class MyGuadanOperateOneFragment : BaseFragment<FragmentLoadmorepageBinding, TradeDetailModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_loadmorepage
    }

    var isLoadMore = false
//    var detailType: String? = ""

    val adapter: MyGuaDAndSgAdapter by lazy {
        MyGuaDAndSgAdapter(mImgLoader).apply {
            isShowCancle = true
            refreshLoading = {
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_LOADING
                }
            }
            refreshNotLoading = {
                binding.refreshLayout.isRefreshing = false
                if (itemCount == 0) {
                    binding.emptyLayout.errorType = EmptyLayout.NODATA
                } else {
                    binding.emptyLayout.visibility = View.GONE
                }
            }
            refreshError = {
                if (itemCount == 0)
                    binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                toast((it).error.message)
            }
            appendError = {
                isLoadMore = true
            }
            cOrder = {
                MsgTitleDialog(requireContext()).apply {
                    okTxt = "撤销"
                    cancleTxt = "不撤销"
                    title = "确认撤销？"
                    ok = {
                        viewModel.tradeRevokeOrder(it)
                    }
                }.show()
            }

        }
    }

    override fun initViewModel(): TradeDetailModel = createViewModel()

    override fun initWidget() {
//        detailType = (requireActivity() as MyGuaDanActivity).detail_type
        binding.emptyLayout.setOnClickListener {
            adapter.refresh()
        }

        binding.recyclerView.adapter = adapter.withLoadStateFooter(PagerFooterAdapter())
        binding.refreshLayout.setOnRefreshListener {
            adapter.refresh()
        }
    }

    override fun onResume() {
        super.onResume()
        lifecycleScope.launchWhenResumed {
            viewModel.pagingDataAuto(20, hold = {
                var ret = viewModel.server.trade_my_order(it, "0").body()
                PagingJsonResult(
                    list = ret?.data?.order_list,
                    code = ret?.code,
                    message = ret?.message
                )

            }).collect {
                adapter.submitData(it)
            }
        }
    }

    override fun initData() {
        viewModel.revokeRet.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    toast("已撤销")
                    adapter.refresh()
                } else {
                    toast(it.message)
                }
                viewModel.revokeRet.value = null
            }
        })
    }



}